home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue30 / corner / LISTING1.PAS next >
Encoding:
Pascal/Delphi Source File  |  1998-01-09  |  3.2 KB  |  123 lines

  1. unit clLockWindow;
  2. interface
  3. uses Windows,Classes,Forms,Controls,Messages;
  4. type
  5.   TLockedItem = Class
  6.   private
  7.     fControl:  TWinControl;
  8.     fHandle:   THandle;
  9.     fRefCount: integer;
  10.   protected
  11.     procedure SetLock; virtual;
  12.     procedure ResetLock; virtual;
  13.   public
  14.     constructor Create(AControl: TWinControl);
  15.     destructor  Destroy; override;
  16.     property Control: TWinControl read fControl;
  17.     property RefCount:    integer read fRefCount write fRefCount;
  18.   end;
  19.   TLockWindow = Class
  20.   private
  21.     fList:   TList;
  22.     fCursor: TCursor;
  23.   protected
  24.     procedure DeleteLock(index: integer); virtual;
  25.     procedure SetCursor; virtual;
  26.     procedure ResetCursor; virtual;
  27.     function  UpdateCount(index,delta: integer): Integer; virtual;
  28.   public
  29.     constructor Create; virtual;
  30.     destructor  Destroy; override;
  31.     function  IndexOf(Item: Pointer): Integer; virtual;
  32.     procedure Lock(AControl: TWinControl); virtual;
  33.     procedure Unlock(AControl: TWinControl); virtual;
  34.     procedure UnlockAll; virtual;
  35.   end;
  36. implementation
  37. constructor TLockedItem.Create(AControl: TWinControl);
  38. begin
  39.   inherited Create;
  40.   fControl := AControl;
  41.   if fControl=nil then fHandle := 0
  42.     else fHandle := fControl.Handle;
  43.   SetLock;
  44. end;
  45.  
  46. destructor TLockedItem.Destroy;
  47. begin
  48.   ResetLock;
  49.   inherited Destroy;
  50. end;
  51. procedure TLockedItem.SetLock;
  52. begin
  53.   if fHandle <> 0 then SendMessage(fHandle,WM_SETREDRAW,0,0);
  54.   fRefCount := 1;
  55. end;
  56. procedure TLockedItem.ResetLock;
  57. begin
  58.   if fHandle <> 0 then begin
  59.     SendMessage(fHandle,WM_SETREDRAW,1,0);
  60.     RedrawWindow(fHandle,nil,0,RDW_FRAME+RDW_INVALIDATE
  61.                               +RDW_ALLCHILDREN+RDW_NOINTERNALPAINT);
  62.   end;
  63. end;
  64. constructor TLockWindow.Create;
  65. begin
  66.   inherited Create;
  67.   fList := TList.Create;
  68. end;
  69. destructor TLockWindow.Destroy;
  70. begin
  71.   UnlockAll;
  72.   fList.Free;
  73.   inherited Destroy;
  74. end;
  75. procedure TLockWindow.SetCursor;
  76. begin
  77.   fCursor := Screen.Cursor;
  78.   Screen.Cursor := crHourGlass;
  79. end;
  80. procedure TLockWindow.ResetCursor;
  81. begin
  82.   Screen.Cursor := fCursor;
  83. end;
  84. function TLockWindow.UpdateCount(index,delta: integer): Integer;
  85. var item: TLockedItem;
  86. begin
  87.   item := TLockedItem(fList[index]);
  88.   item.RefCount := item.RefCount+delta;
  89.   Result := item.RefCount;
  90. end;
  91. function TLockWindow.IndexOf(Item: Pointer): Integer;
  92. begin
  93.   for Result := 0 to fList.Count-1 do
  94.     if TLockedItem(fList[Result]).Control=Item then exit;
  95.   Result := -1;
  96. end;
  97. procedure TLockWindow.Lock(AControl: TWinControl);
  98. var ix: integer;
  99. begin
  100.   if fList.Count=0 then SetCursor;
  101.   ix := IndexOf(AControl);
  102.   if ix < 0 then fList.Add(TLockedItem.Create(AControl))
  103.             else UpdateCount(ix,1);
  104. end;
  105. procedure TLockWindow.DeleteLock(index: integer);
  106. begin
  107.   TLockedItem(fList[index]).Free;
  108.   fList.Delete(index);
  109.   if fList.Count=0 then ResetCursor;
  110. end;
  111. procedure TLockWindow.Unlock(AControl: TWinControl);
  112. var ix: integer;
  113. begin
  114.   ix := IndexOf(AControl);
  115.   if (0 <= ix) and (UpdateCount(ix,-1) < 1) then DeleteLock(ix);
  116. end;
  117. procedure TLockWindow.UnlockAll;
  118. var ix: integer;
  119. begin
  120.   for ix := fList.Count-1 downto 0 do DeleteLock(ix);
  121. end;
  122. end.
  123.